package com.qyhl.webtv.module_news.news.jlnews.multi.fragment;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.JLMultiNewsListBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class JLMultiNewsItemFragment extends BaseFragment implements JLMultiNewsItemContract.JLMultiNewsItemView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private int cityId;

    private View rootView;

    private JLMultiNewsItemPresenter mPresenter;

    private CommonAdapter commonAdapter;

    private List<JLMultiNewsListBean> mDataList = new ArrayList<>();

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    public static JLMultiNewsItemFragment newInstance(int cityId) {
        JLMultiNewsItemFragment fragment = new JLMultiNewsItemFragment();
        fragment.setCityId(cityId);
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return rootView = inflater.inflate(R.layout.news_fragment_jl_multi_item, container, false);
    }

    @Override
    protected void create() {
        mPresenter = new JLMultiNewsItemPresenter(this);
        initView();
    }

    @Override
    protected void setListener() {
        setListeners();
    }

    @Override
    protected void onFirstVisible() {
        mPresenter.getList(cityId);
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);
        loadMask.setReloadButtonText("点击重试~");

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.addItemDecoration(new com.luck.picture.lib.decoration.RecycleViewDivider(
                getContext(), LinearLayoutManager.HORIZONTAL, 1, ContextCompat.getColor(getContext(), R.color.global_gray_lv3)));

        recycleView.setAdapter(commonAdapter = new CommonAdapter<JLMultiNewsListBean>(getContext(), R.layout.news_item_jl_multi_news, mDataList) {
            @Override
            protected void convert(ViewHolder holder, JLMultiNewsListBean bean, int position) {
                ImageView imageView = holder.getView(R.id.cover);
                Glide.with(getContext())
                        .load(bean.getCover())
                        .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default))
                        .into(imageView);

                holder.setText(R.id.title, bean.getName());
            }
        });
    }

    private void setListeners() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getList(cityId);
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (StringUtils.isEmpty(mDataList.get(position).getMcrosite_url())) {
                    showToast("暂未开通融合号", 4);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("url", mDataList.get(position).getMcrosite_url());
                    bundle.putString("title",mDataList.get(position).getName());
                    bundle.putBoolean("hasShare",false);
                    RouterManager.router(ARouterPathConstant.ADV,bundle);

                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }


    @Override
    public void setList(List<JLMultiNewsListBean> beanList) {
        loadMask.setReloadButtonText("点击重试~");
        loadMask.setStatus(LoadingLayout.Success);
        mDataList.clear();
        mDataList.addAll(beanList);

        commonAdapter.notifyDataSetChanged();

    }

    @Override
    public void setError(int code) {
        loadMask.setReloadButtonText("点击重试~");
        if (code == 0) {
            loadMask.setStatus(LoadingLayout.Empty);
            loadMask.setEmptyText("暂无任何内容");
        } else {
            loadMask.setStatus(LoadingLayout.Error);
            loadMask.setEmptyText("获取数据失败！");
        }
    }
}
